//
//  WSJCareNursingPeopleView.m
//  WSJCareNursingPeopleView
//
//  Created by wsj2015 on 16/4/8.
//  Copyright © 2016年 wsj2015. All rights reserved.
//

#import "WSJCareNursingPeopleView.h"
#import "WSJCareNursingPeopleCell.h"
#import "WSJWaitingNursingPeopleView.h"
#import "WCNNurseModel.h"
@interface WSJCareNursingPeopleView ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic,  strong) UICollectionView *collectionView;

@property (nonatomic,  strong) UICollectionViewFlowLayout *layout;



@property (nonatomic,  strong) WSJWaitingNursingPeopleView *expendView;
@property (nonatomic, strong) NSMutableArray *mutiArray;

@end

@implementation WSJCareNursingPeopleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor =[UIColor clearColor];
        UICollectionViewFlowLayout *layout =[[UICollectionViewFlowLayout alloc] init];
        
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        
        self.layout = layout;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        UICollectionView *coll =[[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        coll.backgroundColor = [UIColor clearColor];
        self.collectionView = coll;
        [self.collectionView registerNib:[UINib nibWithNibName:@"WSJCareNursingPeopleCell" bundle:nil] forCellWithReuseIdentifier:@"WSJCareNursingPeopleCell"];
        coll.dataSource = self;
        coll.delegate = self;
        coll.alwaysBounceHorizontal = YES;
        [self addSubview:coll];
    }
    return self;
}



- (void)layoutSubviews
{
    CGFloat w =self.bounds.size.width;
    CGFloat h =self.bounds.size.height;
    self.layout.itemSize = CGSizeMake(80, 98);
    self.layout.minimumInteritemSpacing =0;
    self.layout.minimumLineSpacing= 0;
    self.collectionView.frame =CGRectMake(0, 0, w, 98);
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    if (self.expendView) {
        self.expendView.frame =CGRectMake(0, 98, w, 98 + 34);
    }
    
    
    
    [super layoutSubviews];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.mutiArray) {
       return  self.mutiArray.count+1;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    __weak typeof(self) weakSelf = self;
    
    WSJCareNursingPeopleCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"WSJCareNursingPeopleCell" forIndexPath:indexPath];
    if (indexPath.row==0) {
        cell.userInteractionEnabled = NO;
    }else{
         cell.userInteractionEnabled = YES;
    }
    cell.backgroundColor =[UIColor clearColor];
    if (indexPath.item ==self.mutiArray.count) {
        cell.headIcon.image =[UIImage imageNamed:@"Nurse_add"];
     
        
        cell.nameLabel.hidden = YES;
        cell.tagImageView.hidden = YES;
        cell.deleBtn.hidden = YES;
    }else{
        if (indexPath.item ==0) {
            cell.tagImageView.hidden =NO;
            cell.deleBtn.hidden = YES;
        }else{
            cell.tagImageView.hidden =YES;
            cell.deleBtn.hidden = NO;
        }
        
        WCNNurseModel *model = self.mutiArray[indexPath.item];
        cell.nameLabel.hidden =NO;
        cell.model = model;
    
        cell.deleBlock  =^(){
            NSInteger index = indexPath.item;
            [weakSelf.mutiArray removeObjectAtIndex:index];
            [weakSelf.collectionView deleteItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:indexPath.item inSection:0]]];
            [weakSelf.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
//            [weakSelf.collectionView reloadData];
            if (weakSelf.deleNurse) {
                
                weakSelf.deleNurse(model);
            }
        };
    
    }
   
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.item ==self.mutiArray.count) {
        NSLog(@"zzzzzz");
        if (self.addNewBlok) {
            self.addNewBlok();
        }
    }
    if (indexPath.item ==0) {
        if (self.changeMainNurse) {
            self.changeMainNurse();
        }
    }
    
   
}
- (void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    self.mutiArray =[NSMutableArray array];
    NSMutableArray *uncomfirArray =[NSMutableArray array];
    
    for (WCNNurseModel  *nurse in dataArray) {
        if (nurse.requestStatus==0 && nurse.isPrimary ==NO) {
            [uncomfirArray addObject:nurse];
        }else{
            [self.mutiArray addObject:nurse];
        }
    }
   
    if (!self.expendView && uncomfirArray.count>0) {
         self.expendView =[[WSJWaitingNursingPeopleView alloc] init];
        self.expendView.dataArray =uncomfirArray;
         [self addSubview:self.expendView];
    }
   
    
    
    [self.collectionView reloadData];
}
//- (void)setUnConfrimArray:(NSArray *)unConfrimArray{
//    _unConfrimArray = unConfrimArray;
//    self.expendView =[[WSJWaitingNursingPeopleView alloc] init];
//    self.expendView.dataArray =unConfrimArray;
//    [self addSubview:self.expendView];
//}

@end
